package com.erlin.common.interceptor;

import cn.hutool.core.convert.Convert;
import com.erlin.common.utils.PageParamUtil;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author alin
 */
@Component
public class PageParamInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        Integer pageNo = Convert.toInt(request.getParameter(PageParamUtil.PAGE_NO), PageParamUtil.DEFAULT_PAGE_NO);
        Integer pageSize = Convert.toInt(request.getParameter(PageParamUtil.PAGE_SIZE), PageParamUtil.DEFAULT_PAGE_SIZE);
        // 设置分页参数
        PageParamUtil.setPageParam(pageNo, pageSize);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        PageParamUtil.remove();
    }
}
